package com.farm.area.service.impl;

import com.farm.area.dao.FarmAreaMapper;
import com.farm.area.dao.FarmLandDeviceMapper;
import com.farm.area.pojo.FarmArea;
import com.farm.area.pojo.FarmLand;
import com.farm.area.service.FarmAreaService;
import com.farm.entity.PageResult;
import com.farm.opjo.Crops;
import com.farm.shop.pojo.CustDevice;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author azhe
 * @create 2020/10/10
 */
@Service
public class FarmAreaServiceImpl implements FarmAreaService {

    @Autowired
    private FarmAreaMapper farmAreaMapper;

    @Autowired
    private FarmLandDeviceMapper farmLandDeviceMapper;

    /**
     * 查询所有片区
     * @return
     */
    @Override
    public List<Map<String, Object>> findAll() {
        return farmAreaMapper.findAll();
    }

    /**
     * 修改片区
     * @param farmArea
     */
    @Override
    public void update(FarmArea farmArea) {
        farmAreaMapper.updateByPrimaryKeySelective(farmArea);
    }

    /**
     * 分页条件查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageResult findPage(Integer page, Integer size, Map searchMap) {

        PageHelper.startPage(page,size);
        Example example = new Example(FarmArea.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap != null){
            if(searchMap.get("address") != null && !"".equals(searchMap.get("address"))){
                criteria.andLike("address","%"+searchMap.get("address")+"%");
            }
            if(searchMap.get("name") != null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
            }
            if(searchMap.get("code") != null && !"".equals(searchMap.get("code"))){
                criteria.andLike("code","%"+searchMap.get("code")+"%");
            }
        }
        Page<FarmArea> farmAreaPage = (Page<FarmArea>) farmAreaMapper.selectByExample(example);
        for (FarmArea farmArea : farmAreaPage) {
            Integer id = farmArea.getId();
            List<FarmLand> farmLandList = farmAreaMapper.findFarmLandById(id);
            for (FarmLand farmLand : farmLandList) {
                //查询农作物
                Integer farmLandId = farmLand.getId();
                Crops crops = farmLandDeviceMapper.findCropsByFarmLandId(farmLandId);
                farmLand.setCrops(crops);
                //查询设备
                List<CustDevice> devices = farmLandDeviceMapper.findDevicesByFarmLandId(farmLandId);
                farmLand.setDevices(devices);
            }

            farmArea.setFarmLands(farmLandList);
        }
        return new PageResult(farmAreaPage.getTotal(),farmAreaPage.getResult());
    }

    /**
     * 根据id删除片区
     * @param id
     */
    @Override
    public void delete(Integer id) {
        FarmArea farmArea = farmAreaMapper.selectByPrimaryKey(id);
        farmArea.setStatus("1");
        farmAreaMapper.updateByPrimaryKeySelective(farmArea);
    }

    /**
     * 新增片区
     * @param farmArea
     */
    @Override
    public void add(FarmArea farmArea) {
        //新增农场，将农场的状态设为可用   0为可用
        farmArea.setStatus("0");
        farmAreaMapper.insertSelective(farmArea);
    }
}
